<template>
  <div class="modal">
    <div class="set-account" v-loading="loading">
      <div class="modal-header">
        <i></i>
        <span>批量生成教师账号</span>
        <i class="el-icon-close" @click="$emit('close')"></i>
      </div>
      <div class="modal-body">
        <div class="row">
          <span class="label">生成规则</span>
          <div class="content">
            <el-radio v-model="type" :label="1">按手机号</el-radio>
            <el-radio v-model="type" :label="2">按姓名</el-radio>
          </div>
        </div>
        <div class="row">
          <span class="label">账号</span>
          <el-input
            v-model="prefix"
            placeholder="请输入3-5位英文字母 (大小写均可)"
            maxlength="5"
            clearable
          ></el-input>
        </div>
        <p>示例：Abcd，确认后将生成以下老师账号</p>
        <p v-if="type ===1">Abcd+年级+手机号后4位</p>
        <p v-else>Abcd+教师姓名</p>
      </div>
      <div class="modal-footer">
        <div class="cancel-btn btn" @click="$emit('close')">取消</div>
        <div class="confirm-btn btn" @click="confirm">确认</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
  name: 'ResetPassword',
  data() {
    return {
      loading: false,
      prefix: '',
      type: 1,
    };
  },
  computed: {
    ...mapGetters('school', ['schoolId']),
  },

  methods: {
    async confirm() {
      if (this.prefix.length < 3) {
        this.$toast('请输入的账号前缀为3~5位');
        return;
      }
      this.loading = true;
      try {
        const formData = new FormData();
        formData.append('F_school_id', this.schoolId);
        formData.append('F_prefix', this.prefix);
        formData.append('F_type', this.type);
        const { F_responseNo, F_responseMsg } = await this.$api.setAccount(
          formData
        );
        if (F_responseNo === 10000) {
          this.$toast('一键设置账号成功');
          this.$emit('update');
          this.$emit('close');
        } else {
          this.$toast('一键设置账号失败:' + F_responseMsg);
          this.$emit('close');
        }
      } catch (e) {
        console.error(e);
        this.$toast('一键设置账号失败，请联系后台成员');
        this.$emit('close');
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.set-account {
  display: flex;
  flex-direction: column;
  width: 600px;
  height: 350px;
  position: absolute;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  font-size: 16px;
  color: #666;
  border-radius: 6px;
  background-color: #fff;

  .modal-header {
    padding: 0 20px;
    height: 45px;
    font-size: 18px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    i {
      cursor: pointer;
    }
  }
  .modal-body {
    box-sizing: border-box;
    padding: 20px 40px;
    flex: 1;
    text-align: left;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    color: #666;
    .row {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      .label {
        flex: 0 0 auto;
        width: 100px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background-color: #f0f0f0;
      }
      .content {
        padding-left: 20px;
      }
    }
    p {
      height: 30px;
      line-height: 30px;
    }
  }

  .modal-footer {
    height: 70px;
    display: flex;
    justify-content: center;
    align-items: center;

    .btn {
      width: 80px;
      height: 34px;
      line-height: 34px;
    }
    .cancel-btn {
      margin-right: 20px;
      background-color: #fff;
      border: solid 2px #f5f5f5;
      color: #b3b3b3;
    }
    .confirm-btn {
      background-color: #009cff;
      color: #fff;
    }
  }
}
</style>
